package com.aaa.marketresearch.webclient.gwt.domain;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;

import java.util.Date;

/**
 * @author Nail
 */
public class FamilyMember {

    private static final String BIRTHDAY_DATE_FORMAT = "dd-MM-yyyy";
    private static final DateTimeFormat birthdayDateFormat = DateTimeFormat.getFormat(BIRTHDAY_DATE_FORMAT);


    private Integer id;
    private Integer familyId;
    private String name;
    private String gender;
    private String profession;
    private Date birthday;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getFamilyId() {
        return familyId;
    }

    public void setFamilyId(Integer familyId) {
        this.familyId = familyId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public Date getBirthday() {
        return birthday;
    }

    public String getTextBirthday() {
        return birthdayDateFormat.format(this.birthday);
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void setBirthday(String birthdayDate) {
        this.birthday = birthdayDateFormat.parse(birthdayDate);
    }

    public FamilyMember() {
        this.id = null;
        this.name = "";
        this.gender = "";
        this.profession = "";
        String birthdayDate = "30-01-1980";
        this.birthday = birthdayDateFormat.parse(birthdayDate);
    }

    public FamilyMember(JSONObject jObj) {
        this.id = (int)jObj.get("id").isNumber().doubleValue();
        this.familyId = (int)jObj.get("familyId").isNumber().doubleValue();
        this.name = jObj.get("name").isString().stringValue();
        this.gender = jObj.get("gender").isString().stringValue();
        this.profession = jObj.get("profession").isString().stringValue();

        String birthdayDate = jObj.get("birthday").isString().stringValue();
        this.birthday = birthdayDateFormat.parse(birthdayDate);
    }

    public JSONObject getJSON() {
        JSONObject jObj = new JSONObject();
        if (this.id != null) jObj.put("id", new JSONNumber(this.id));
        jObj.put("familyId", new JSONNumber(this.familyId));
        jObj.put("name", new JSONString(this.name));
        jObj.put("gender", new JSONString(this.gender));
        jObj.put("profession", new JSONString(this.profession));

        String birthdayDate = null;
        birthdayDate = birthdayDateFormat.format(this.birthday);
        if (birthdayDate != null) {
            jObj.put("birthday", new JSONString(birthdayDate));
        }
        return jObj;
    }
}